#include "qeditcomboboxitems.h"
#include "ui_qeditcomboboxitems.h"

#include "../../include/qcommonstruct.h"

#include <QVBoxLayout>
#include <QHBoxLayout>


QItemList::QItemList(QWidget *parent):
    QTreeWidget(parent)
{
    setColumnCount(2);
    setHeaderLabels(QStringList()<<"text"<<"icon");
    this->setColumnWidth(0,140);
    this->setEditTriggers(QAbstractItemView::DoubleClicked);

    connect(this,SIGNAL(pressed(QModelIndex)),this,SLOT(editCurrent(QModelIndex)));
}

void QItemList::setValue(const QVariant &v)
{
    this->clear();
    m_items.clear();

    ComboItems items=v.value<ComboItems>();
    tagComboItem item;
    QTreeWidgetItem *widItem;
    for(int i=0;i<items.size();i++)
    {
        item=items.at(i);
        widItem=new QTreeWidgetItem(this);
        widItem->setText(0,item.m_text);
        widItem->setText(1,item.m_icon);
        widItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
        m_items.append(widItem);
    }
    if(m_items.size()>0)
    {
        m_items.first()->setSelected(true);
    }
}

void QItemList::editCurrent(const QModelIndex &index)
{
    edit(index);
}

QVariant QItemList::value()
{
    ComboItems items;
    tagComboItem item;

    foreach(QTreeWidgetItem *widItem,m_items)
    {
        item.m_icon=widItem->text(1);
        item.m_text=widItem->text(0);
        items.append(item);
    }

    QVariant v;
    v.setValue<ComboItems>(items);
    return v;
}

void QItemList::addItem()
{
    QTreeWidgetItem *widItem;
    widItem=new QTreeWidgetItem(this);
    widItem->setText(0,"string");
    widItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
    m_items.append(widItem);
    this->clearSelection();
    widItem->setSelected(true);
}

void QItemList::removeSelect()
{
    QList<QTreeWidgetItem*> items=this->selectedItems();
    qDeleteAll(items);
}


QEditComboBoxItems::QEditComboBoxItems(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QEditComboBoxItems),
    m_itemList(new QItemList(this)),
    m_okBtn(new QPushButton(this)),
    m_addBtn(new QPushButton(this)),
    m_delBtn(new QPushButton(this)),
    m_ret(0)
{
    ui->setupUi(this);

    QVBoxLayout *l=new QVBoxLayout;
    l->setSpacing(0);
    l->setMargin(0);
    l->addWidget(m_itemList);

    QWidget *wid=new QWidget(this);

    QHBoxLayout *h=new QHBoxLayout;

    wid->setLayout(h);
    h->addWidget(m_addBtn);
    h->addWidget(m_delBtn);
    h->addWidget(m_okBtn);
    l->addWidget(wid);

    m_okBtn->setText("Ok");
    m_okBtn->setFixedWidth(80);

    m_addBtn->setText("Add");
    m_addBtn->setFixedWidth(80);

    m_delBtn->setText("Remove");
    m_delBtn->setFixedWidth(80);
    this->setLayout(l);

    connect(m_addBtn,SIGNAL(clicked()),m_itemList,SLOT(addItem()));
    connect(m_delBtn,SIGNAL(clicked()),m_itemList,SLOT(removeSelect()));
    connect(m_okBtn,SIGNAL(clicked()),this,SLOT(okClicked()));
}

QEditComboBoxItems::~QEditComboBoxItems()
{
    delete ui;
}

void QEditComboBoxItems::setValue(const QVariant &v)
{
    m_itemList->setValue(v);
}

QVariant QEditComboBoxItems::value()
{
    return m_itemList->value();
}

void QEditComboBoxItems::okClicked()
{
    m_ret=1;
    close();
}

int QEditComboBoxItems::exec()
{
    QDialog::exec();
    return m_ret;
}
